<template>
	<!-- <el-button style="float: right;" @click="toExport" type="primary">导出报告</el-button> -->
	<div class="table_part" v-loading="pageLoading">
		<center><h1>老年人能力评估报告</h1></center>
		<table width="100%" border cellpadding="5" cellspacing="0">
			<tr>
				<td>{{ otherTableData.data[0].tableTitle }}</td>
				<td>总分：{{ otherTableData.data[0].totalScore || 0 }}分</td>
				<td>{{ otherTableData.data[1].tableTitle }}</td>
				<td>总分：{{ otherTableData.data[1].totalScore || 0 }}分</td>
			</tr>
			<tr>
				<td>{{ otherTableData.data[2].tableTitle }}</td>
				<td>总分：{{ otherTableData.data[2].totalScore || 0 }}分</td>
				<td>{{ otherTableData.data[3].tableTitle }}</td>
				<td>总分：{{ otherTableData.data[3].totalScore || 0 }}分</td>
			</tr>
			<tr>
				<td>老年人能力初步等级</td>
				<td>{{ data.firstAbilityLevelLabel }}</td>
				<td>评估编号</td>
				<td>{{ data.assessCode }}</td>
			</tr>
			<tr>
				<td>能力等级变更依据</td>
				<td>{{ data.changeAbilityBasisLabel }}</td>
				<td>评估基准日期</td>
				<td>{{ data.statisticalDate }}</td>
			</tr>
			<tr>
				<td>最终的等级</td>
				<td>{{ data.finalAbilityLevelLabel }}</td>
				<td>评估原因</td>
				<td>
					<div>
						{{ data.assessReasonLabel }}
					</div>
					<div v-if="data.assessReasonOther">其他原因:{{ data.assessReasonOther }}</div>
				</td>
			</tr>
			<tr>
				<th style="text-align: center" colspan="4">基本信息</th>
			</tr>
			<tr>
				<td>姓名</td>
				<td>{{ data.oldmanName }}</td>
				<td>性别</td>
				<td>{{ data.genderLabel }}</td>
			</tr>
			<tr>
				<td>公民身份证号</td>
				<td>{{ data.idCard }}</td>
				<td>评估地点</td>
				<td>{{ data.assessAddress }}</td>
			</tr>
			<tr>
				<td>身高（cm）</td>
				<td>{{ data.oldmanHeight }}</td>
				<td>体重（KG）</td>
				<td>{{ data.oldmanWeight }}</td>
			</tr>
			<tr>
				<td>民族</td>
				<td>{{ data.nationTypeLabel }}</td>
				<td>宗教信仰</td>
				<td>{{ data.faithTypeLabel }}</td>
			</tr>
			<tr>
				<td>文化程度</td>
				<td>{{ data.eduTypeLabel }}</td>
				<td>婚姻状况</td>
				<td>{{ data.marriageLabel }}</td>
			</tr>
			<tr>
				<td>医疗费用支付方式（多选）</td>
				<td>{{ data.payWayLabels }}</td>
				<td>经济来源（多选）</td>
				<td>{{ data.economicSourceLabels }}</td>
			</tr>
			<tr>
				<td>居住情况（多选）</td>
				<td colspan="3">{{ data.livingTypeLabels }}</td>
			</tr>
			<tr>
				<td rowspan="5">近30天内照护风险事件</td>
				<td>跌倒</td>
				<td colspan="2">{{ data.fallLabel }}</td>
			</tr>
			<tr>
				<td>走失</td>
				<td colspan="2">{{ data.lostLabel }}</td>
			</tr>
			<tr>
				<td>噎食</td>
				<td colspan="2">{{ data.chokingLabel }}</td>
			</tr>
			<tr>
				<td>自杀、自伤</td>
				<td colspan="2">{{ data.suicideLabel }}</td>
			</tr>
			<tr>
				<td>其他</td>
				<td colspan="2">{{ data.otherLabel }}</td>
			</tr>
			<tr>
				<th style="text-align: center" colspan="4">信息提供者以及联系人信息</th>
			</tr>
			<tr>
				<td>信息提供者的姓名</td>
				<td>{{ data.provideName }}</td>
				<td>信息提供者与老年人关系</td>
				<td>{{ data.provideRelationLabel }}</td>
			</tr>
			<tr>
				<td>联系人姓名</td>
				<td>{{ data.contactsName }}</td>
				<td>联系人电话</td>
				<td>{{ data.contactsPhone }}</td>
			</tr>
		</table>
		<!-- A4：疾病诊断和用药情况表 -->
		<table width="100%" border cellpadding="5" cellspacing="0">
			<caption style="font-weight: 700">
				A4：疾病诊断和用药情况表
			</caption>
			<tr>
				<td>A.4.1：疾病诊断（可多选）</td>
				<td colspan="4">{{ judgetable1Result(table1Data[0]) }}</td>
			</tr>
			<tr>
				<td rowspan="5">A.4.2：用药情况（目前长期服药情况）</td>
				<td>药物名称</td>
				<td>服药方法</td>
				<td>用药剂量</td>
				<td>用药频率</td>
			</tr>
			<tr style="height: 30px" v-for="num in table1Data[1].lineNumber">
				<td>{{ table1Data[1].bscAssessTableAnswer?table1Data[1].bscAssessTableAnswer.content['bb75e7edf53c546b8f38aec930f8442d'][num-1]: '' }}</td>
				<td>{{ table1Data[1].bscAssessTableAnswer?table1Data[1].bscAssessTableAnswer.content['2adf0502f57ef7a230587d82f2012aad'][num-1]: '' }}</td>
				<td>{{ table1Data[1].bscAssessTableAnswer?table1Data[1].bscAssessTableAnswer.content['4130e66e24a4de78bf8e47380d9282c1'][num-1]: '' }}</td>
				<td>{{ table1Data[1].bscAssessTableAnswer?table1Data[1].bscAssessTableAnswer.content['9887d24cae3346e8056b194528b7f4be'][num-1]: '' }}</td>
			</tr>
		</table>
		<!-- A5：健康相关情况表 -->
		<table width="100%" border cellpadding="5" cellspacing="0">
			<caption style="font-weight: 700">
				A5：健康相关情况表
			</caption>
			<tr v-for="(item, index) in table2Data" :key="index">
				<td>{{ item.itemTitle }}</td>
				<td style="width: 50%;">{{ judgeResult(item) }}</td>
			</tr>
		</table>
	</div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
// import axios from 'axios'
// import FileSaver from 'file-saver'
import apiMod from '@/apiMod/index'
import { useCommonStore } from '@/stores/common'
const { setLoading } = useCommonStore()

type Props = {
	nowRowData?: any
}
const props = withDefaults(defineProps<Props>(), {
	nowRowData: {},
})
let pageLoading = ref(true)
let data: any = ref({})
let table1Data: any = ref([
	{
		bscAssessTableAnswer: {},
		optionList: [],
	},
	{
		bscAssessTableAnswer: {
			content: {
				bb75e7edf53c546b8f38aec930f8442d: ['', '', '', ''],
				'2adf0502f57ef7a230587d82f2012aad': ['', '', '', ''],
				'4130e66e24a4de78bf8e47380d9282c1': ['', '', '', ''],
				'9887d24cae3346e8056b194528b7f4be': ['', '', '', ''],
			},
		},
	},
])
let table2Data: any = ref({})

// 获取老年人基础信息
let otherTableData: any = ref({
	data: [
		{
			id: '',
			tableDescribe: '',
			tableTitle: '',
			totalScore: '',
		},
		{
			id: '',
			tableDescribe: '',
			tableTitle: '',
			totalScore: '',
		},
		{
			id: '',
			tableDescribe: '',
			tableTitle: '',
			totalScore: '',
		},
		{
			id: '',
			tableDescribe: '',
			tableTitle: '',
			totalScore: '',
		},
	],
})
// 获取A4：疾病诊断和用药情况表数据 // 获取A5：健康相关情况表
// 8dad8d430bffb0c66de5702f960f8072
const getTable1Data = async (assessCode, tableId) => {
	
	let res = await apiMod.view_bscAssessTableItem_page({
		assessCode: assessCode,
		assessType: 1,
		tableId: tableId,
		pageParaPO: { size: 1000000, current: 1 },
		taskId: props.nowRowData.taskId,
		taskItemId: props.nowRowData.taskItemId,
	})
	return res.data.records
}
const getBaseInfo = async () => {
	// apiMod.view_bscElderlyAssessBaseInfo_page({ idCard: props.nowRowData.idCard, oldmanName: props.nowRowData.oldmanName }).then( async(res)=>{
	// console.log('得到基础信息',res)

	data.value = props.nowRowData
	console.log('得到基础信息', props.nowRowData)
	setLoading(true)
	table1Data.value = await getTable1Data(data.value.assessCode, '8dad8d430bffb0c66de5702f960f8072')
	table2Data.value = await getTable1Data(data.value.assessCode, '476c439894c3cd9da35aba349e83df2b')
	setLoading(false)
	otherTableData.value = await apiMod.view_bscElderlyAssessBaseInfo_assessTablesTotalScore({ assessCode: data.value.assessCode })
	
	if(table1Data.value[1].bscAssessTableAnswer){
		table1Data.value[1].bscAssessTableAnswer.content = JSON.parse(table1Data.value[1].bscAssessTableAnswer.content)
	}
	
	
	// })
	pageLoading.value = false
}
getBaseInfo()

const judgeResult = (data) => {
	if (data.bscAssessTableAnswer) {
		if (data.itemType == 1) {
			let res = data.optionList.find((el) => {
				return el.id == data.bscAssessTableAnswer.content
			})
			if (res) {
				return res.optionContent
			}
		} else if (data.itemType == 2) {
			let list = data.bscAssessTableAnswer.content.split(',')
			let answerTextList = []
			data.optionList.forEach((el) => {
				list.forEach((item) => {
					if (item == el.id) {
						answerTextList.push(el.optionContent)
					}
				})
			})
			return answerTextList.join(',')
		} else if (data.itemType == 3) {
			// 填空
			if (data.bscAssessTableAnswer) {
				return data.bscAssessTableAnswer.content
			} else {
				return ''
			}
		}
	}
}
const judgetable1Result = (val) => {
	if (val) {
		let list = val.optionList.map((el) => {
			if (val.bscAssessTableAnswer&&val.bscAssessTableAnswer.content.includes(el.id)) {
				return el.optionContent
			}
		})
		list = list.filter((el) => {
			return el != undefined
		})
		return list.join(',')
	}
}
</script>

<style scoped>
.bottom {
	margin-top: 13px;
	line-height: 12px;
	display: flex;
	justify-content: space-between;
	align-items: center;
}

.detail-label {
	color: #eee;
}

.image {
	width: 90%;
	height: 220px;
	display: block;
}
.table_part {
	width: 100%;
	padding: 10px;
}
</style>
